package com.bdd.controller;

import com.alibaba.druid.util.StringUtils;
import com.bdd.Result.Result;
import com.bdd.entity.User;
import com.bdd.mapper.UserDAO;
import com.bdd.model.bo.PageBO;
import com.bdd.model.bo.UserBO;
import com.bdd.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.login.AccountException;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(value = "用户管理")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    UserDAO userDAO;

    @PostMapping("/login")
    @ApiOperation("登录")
    @ResponseBody
    public Result<UserBO> login(UserBO bo) {
        if (StringUtils.isEmpty(bo.getUsername()) || StringUtils.isEmpty(bo.getPassword())) {
            throw new IncorrectCredentialsException();
        }
        UserBO bo1 = userService.login(bo);

        return Result.ok(bo1);
    }

    @PostMapping("/add")
    @ResponseBody
//    @RequiresRoles({"ADMIN"})
    public Result addUser(UserBO bo) {
        if (StringUtils.isEmpty(bo.getUsername()) || StringUtils.isEmpty(bo.getPassword())) {
            throw new IllegalArgumentException("用户名或密码不可为空");
        }
        if (StringUtils.isEmpty(bo.getRole())) {
            throw new IllegalArgumentException("用户角色不可为空");
        }
        userService.add(bo);
        return Result.ok();
    }

    @PostMapping("/update")
//    @RequiresRoles({"ADMIN"})
    @ResponseBody
    public Result update(UserBO bo) {
        if (ObjectUtils.isEmpty(bo.getId())) {
            throw new IllegalArgumentException("用户id不可为空");
        }
        User user = userDAO.selectByPrimaryKey(bo.getId());
        if (ObjectUtils.isEmpty(user)) {
            throw new IllegalArgumentException("此用户不存在");
        }
        userService.updateUserInfo(bo);
        return Result.ok();
    }

    @PostMapping("/delete")
//    @RequiresRoles({"ADMIN"})
    @ResponseBody
    public Result delete(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            throw new IllegalArgumentException("用户id不可为空");
        }
        User user = userDAO.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(user)) {
            throw new IllegalArgumentException("此用户不存在");
        }
        userService.deleteByUserId(id);
        return Result.ok();
    }

    @GetMapping("/list")
//    @RequiresRoles({"ADMIN"})
    @ResponseBody
    public Result<PageInfo<UserBO>> list(PageBO pageBO){
        if(pageBO.getPageNum()<=0){
            pageBO.setPageNum(1);
        }
        if(pageBO.getPageSize()<=0){
            pageBO.setPageSize(10);
        }
        PageInfo<UserBO> boList = new PageInfo<>(userService.list(pageBO.getPageNum(),pageBO.getPageSize()));
        return Result.ok(boList);
    }


    @RequestMapping("/logout")
    @ResponseBody
    public Result logout(HttpSession session, Model model) {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        model.addAttribute("msg", "安全退出！");
        return Result.ok();
    }


    @PostMapping("/getByNameOrId")
//    @RequiresRoles({"ADMIN"})
    @ResponseBody
    public Result<PageInfo<UserBO>> getByNameOrId(PageBO pageBO,UserBO userBO){
        if(ObjectUtils.isEmpty(pageBO.getPageNum())||pageBO.getPageNum()<=0){
            pageBO.setPageNum(1);
        }
        if(ObjectUtils.isEmpty(pageBO.getPageSize())||pageBO.getPageSize()<=0){
            pageBO.setPageSize(10);
        }
        PageInfo<UserBO> boList = new PageInfo<>(userService.getByNameOrId(pageBO.getPageNum(),pageBO.getPageSize(),userBO));
        return Result.ok(boList);
    }


}
